package com.ruoyi.book.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.book.domain.Book;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.book.domain.Borrow;
import com.ruoyi.book.service.IBorrowService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 借书管理Controller
 *
 * @author ruoyi
 * @date 2022-12-30
 */
@RestController
@RequestMapping("/book/borrow")
public class BorrowController extends BaseController
{
    @Autowired
    private IBorrowService borrowService;


    /**
     * 查询借书管理列表
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:list')")
    @GetMapping("/getListByUserId")
    public AjaxResult getListByUserId(Borrow borrow)
    {
        List<Borrow> list = borrowService.getListByUserId(borrow);
        return AjaxResult.success(list);
    }


    /**
     * 查询借书管理列表
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:list')")
    @GetMapping("/list")
    public TableDataInfo list(Borrow borrow)
    {
        startPage();
        List<Borrow> list = borrowService.selectBorrowList(borrow);
        return getDataTable(list);
    }

    /**
     * 导出借书管理列表
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:export')")
    @Log(title = "借书管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Borrow borrow)
    {
        List<Borrow> list = borrowService.selectBorrowList(borrow);
        ExcelUtil<Borrow> util = new ExcelUtil<Borrow>(Borrow.class);
        util.exportExcel(response, list, "借书管理数据");
    }

    /**
     * 获取借书管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(borrowService.selectBorrowById(id));
    }

    /**
     * 新增借书管理
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:add')")
    @Log(title = "借书管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Book book)
    {
        return toAjax(borrowService.insertBorrow(book));
    }

    /**
     * 修改借书管理
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:edit')")
    @Log(title = "借书管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Borrow borrow)
    {
        return toAjax(borrowService.updateBorrow(borrow));
    }

    /**
     * 删除借书管理
     */
//    @PreAuthorize("@ss.hasPermi('book:borrow:remove')")
    @Log(title = "借书管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(borrowService.deleteBorrowByIds(ids));
    }
}
